<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$post=get_postdata();
	if($post['system']){
		$uniacid=1;
	}else{
		$uniacid=$_SESSION['uniacid'];
	}
	//获取企业微信ID
	if(!$wxworkapp_id){
			$wxworkapp_id=$_SESSION['wxworkapp_id'];
	}
	if(!$wxworkapp_id && $_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core'){
			$pid=pdo_getcolumn('core_plugins',array('name'=>$_SESSION['current_plugin']),'id');
			$wxworkapp_id=pdo_getcolumn('core_account_wxwork_app',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid']),'id');
			$_SESSION['wxworkapp_id']=$wxworkapp_id;
	}
	if(!$wxworkapp_id){
			$wxworkapp_id=pdo_getcolumn('core_account_wxwork_app',array('default'=>1,'uniacid'=>$_SESSION['uniacid']),'id');
			$_SESSION['wxworkapp_id']=$wxworkapp_id;
	}
	if(!$wxworkapp_id){
		exi('');
	}
	$check=pdo_get('core_plugins',array('name'=>'jy_scrm'),'id');
	if($check){
		$workerid=pdo_getcolumn('jy_scrm_worker',array('default'=>1,'uniacid'=>$_SESSION['uniacid']),'userid');
	}
	if(!$workerid){
		exi('');
	}
	$attachment=pdo_getcolumn('core_account_wxwork_ewm',array('wxwork_id'=>$wxworkapp_id,'action'=>$post['url']),'attachment');
	$file = ROOT_AT.$attachment;
	if(!$attachment || !(is_file($file))){
			$params=array(
				'type'=>1,
				'scene'=>2,
				'remark'=>'自动生成',
				'skip_verify'=>false,
				'state'=>30,
				'user'=>$workerid,
				'party'=>'',
				'is_temp'=>false,
				'expires_in'=>0,
				'chat_expires_in'=>0,
				'unionid'=>'',
			);
			$res=model('core/wxwork_userapi',$wxworkapp_id)->add_contact_way($params);
			if(!$res['qr_code']){
				exi('');
			}
			//$attachment=model('core/files')->source_save($res['qr_code'],'data','jpg');
			$insert=array(
				'uniacid'=>$uniacid,
				'wxwork_id'=>$wxworkapp_id,
				'action'=>$post['url'],
				'attachment'=>$res['qr_code'],
			);
			pdo_insert('core_account_wxwork_ewm',$insert);
	}
	$ewm_url=tomedia($res['qr_code']);
	exi($ewm_url);
}